# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import codecs
import time
from scrapy import Request
from itemadapter import ItemAdapter
import json
#保存到本地
from scrapy.pipelines.images import ImagesPipeline

#保存文本文件
class Spider04Pipeline(object):
    def __init__(self):
        self.file = codecs.open('douban.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        # if item["name"] == "getdata04" and  item["grade"] == "getdata04":

        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(line)
        return item

    def spider_closed(self, spider):
        self.file.close()

#保存图片
# class SCRPipeline(ImagesPipeline):
#     default_headers = {
#         'accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
#         'accept-encoding': 'gzip, deflate, br',
#         'accept-language': 'zh-CN,zh;q=0.9',
#         'referer': 'http://item.kongfz.com/Cjisuanji/',
#         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0',
#     }
#     def get_media_requests(self,item,info):
#
#         for image_url in item['url']:
#             self.default_headers['referer'] = image_url
#             yield Request(image_url, headers=self.default_headers)
#
#
#     def file_path(self, request, item, response=None, info=None):
#         # 通过meta的key1获取values1值
#         data = item['name']
#         print('正在保存', data)
#         return data
#
#     def item_completed(self, results, item, info):
#         return item


#桌面保存图片
class MyImagesPipeline(ImagesPipeline):
    # yield meta for file_path() function
    def get_media_requests(self, item, info):
        # if item["url"] == "getdata04":
            for url in item['url']:

                yield Request(url, meta={'item': item, 'index':item['url'].index(url)})

    # rename the image
    def file_path(self, request, response=None, info=None):
        item = request.meta['item']
        index = request.meta['index']
        name = item['name'][index]


        return 'full/%s.jpg' % (name)

